package com.quanquan.management.service;

import DTO.PageDTO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.ConsumptionAmountDTO;
import com.quanquan.management.request.ConsumptionAmountRequestBosy;
import entity.ExcelData;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.DateUtils;
import utils.ExcelUtil;
import utils.Request;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName:ConsumptionAmountService
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/5/9 16:06
 * @Version 1.0
 */
@Service
public class ConsumptionAmountService {

    @Value("${module-url.userUrl}")
    private String userUrl;
    /**
     * 列表
     * @param request
     * @return
     */
    public PageDTO<ConsumptionAmountDTO> list(ConsumptionAmountRequestBosy request) {
        String json = Request.postJson(userUrl+"consumption_amount/list", request);
         JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        PageDTO<ConsumptionAmountDTO> pageDTO = null;
        if (0 == resCode) {
            JSONObject jsonData = jsonObject.getJSONObject("data");
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            JSONArray list = jsonData.getJSONArray("list");
            List<ConsumptionAmountDTO> consumptionAmountDTOS = new LinkedList<>();
            for (int i = 0; i < list.size(); i++ ) {
                ConsumptionAmountDTO object = list.getObject(i, ConsumptionAmountDTO.class);
                consumptionAmountDTOS.add(object);
            }
            data.setList(consumptionAmountDTOS);
            return data;
        }
        return pageDTO;
    }


    public void export(HttpServletResponse response, ConsumptionAmountRequestBosy requestBody) throws Exception {
        List<String> titles = new LinkedList<>();
        titles.add("用户手机号");
        titles.add("用户名称");
        titles.add("金额");
        titles.add("商户名称");
        titles.add("商户业态");
        titles.add("消费日期");
        ExcelData data = ExcelUtil.getExcelData();
        data.setTitles(titles);
        PageDTO<ConsumptionAmountDTO> pageDTO = this.list(requestBody);
        List<ConsumptionAmountDTO> list = pageDTO.getList();
        List<List<Object>> rows = new LinkedList<>();
        list.forEach(vo -> {
            List<Object> row = new LinkedList<>();
            row.add(vo.getPhone());
            row.add(vo.getMemberName());
            row.add(vo.getMoney());
            row.add(vo.getStoreName());
            row.add(vo.getStoreCommercial());
            row.add(vo.getConsumeDay());
            rows.add(row);
        });
        data.setRows(rows);
        String now = DateUtils.currentDate(DateUtils.DATE_HOUR_SECOND);
        ExcelUtil.exportExcel(response,"自助积分记录" + now,data);
    }


}
